#include <iostream>
#include <functional>
using namespace std;
void Add(int x, int y)
{
    cout << "x + y = " << x + y << endl;
}

class myclass
{
public:
    void Add(int x, int y)
    {
        cout << "x + y = " << x + y << endl;
    }

    static void Sub(int x, int y)
    {
        cout << "x - y = " << x - y << endl;
    }
};

int main()
{
    //函数参数绑定
    auto bind1 = bind(&Add, 1, placeholders::_1);
    bind1(2);
    auto bind2 = bind(&Add, 1, 2);
    bind2();
    //成员函数绑定
    myclass cla;
    auto bind3 = bind(&myclass::Add, cla, 1, 2);
    bind3();
    auto bind4 = bind(&myclass::Sub, 2, 1);
    bind4();

    //通过lambda的方式实现bind
    //函数参数的绑定
    cout << "lambda" << endl;
    auto lambda1 = [](int y){Add(1, y);};
    auto lambda2 = [](){Add(1, 2);};
    //成员函数的绑定
    auto lambda3 = [&cla](int y){cla.Add(1, y);};
    lambda1(2);
    lambda2();
    lambda3(2);
    return 0;
}
